package com.etc.order.provider.mapper;

import com.etc.order.common.domain.ProductInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductInfoMapper {
    @Select("SELECT * FROM product_info LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("productInfoMap")
    List<ProductInfo> getAll(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM product_info WHERE product_name LIKE CONCAT('%', #{productName}, '%')")
    int getTotalCount(@Param("productName") String productName);

    @Select("SELECT * FROM product_info WHERE product_name = #{productName} LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("productInfoMap")
    List<ProductInfo> findByProductName(@Param("productName") String productName, @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    /** by lw **/
    @Select("SELECT * FROM product_info WHERE category_type = #{categoryType} AND product_status != 1")
    @ResultMap("productInfoMap")
    List<ProductInfo> getFoods(@Param("categoryType") Integer categoryType);

    @Select("SELECT * FROM product_info WHERE product_id = #{productId}")
    @Results(id = "productInfoMap", value = {
            @Result(column = "product_id", property = "productId"),
            @Result(column = "product_name", property = "productName"),
            @Result(column = "product_price", property = "productPrice"),
            @Result(column = "product_stock", property = "productStock"),
            @Result(column = "product_description", property = "productDescription"),
            @Result(column = "product_icon", property = "productIcon"),
            @Result(column = "product_status", property = "productStatus"),
            @Result(column = "category_type", property = "categoryType"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    ProductInfo getOne(@Param("productId") String productId);

    @Insert("INSERT INTO product_info VALUES(" +
            "#{info.productId}, " +
            "#{info.productName}, " +
            "#{info.productPrice}, " +
            "#{info.productStock}, " +
            "#{info.productDescription}, " +
            "default, " +
            "default, " +
            "#{info.categoryType}, " +
            "now(), " +
            "now()) "
    )
    int save(@Param("info") ProductInfo info);

    @Update("UPDATE product_info SET " +
            "product_name = #{info.productName}, " +
            "product_price = #{info.productPrice}, " +
            "product_description = #{info.productDescription}, " +
            "product_icon = #{info.productIcon}, " +
            "product_status = #{info.productStatus}, " +
            "category_type = #{info.categoryType}, " +
            "update_time = now() WHERE product_id = #{info.productId}"
    )
    int update(@Param("info") ProductInfo info);

    @Delete("DELETE FROM product_info WHERE product_id = #{productId}")
    int delete(@Param("productId") String productId);

    @Update("UPDATE product_info SET product_stock = #{productStock}, update_time = now() WHERE product_id = #{productId}")
    int decreaseStock(@Param("productId") String productId, @Param("productStock") int productStock);
}
